﻿/***********************************************************************************************
 * 
 * ProjectName: WMSSoft.Lib.Basic
 * 
 * Description: Class to Log Errors, Hints and Informations in XML - File
 * 
 * Created:     10.05.2012
 * 
 * Author:      matthias wagler - matthias@WMSSoft.de
 * 
 * Copyright:   Copyright 2012 by WMSSoft.de
 * 
 * License:     GPLv3
 * 
 * History:     10.05.2012 - matthias wagler matthias@WMSSoft.de   -   Created
 * 
 **********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WMSSoft.Lib.Basic
{
    public static class CMessagelogging
    {
        private static string m_sLoggingFilePath = @"WMSSoft\Messages";
        private static string m_sLoggingFilename = "messages.xml";
        private static System.IO.StreamWriter m_LoggingWriter = null;

        private static List<string> m_LogMessages = new List<string>();

        /// <summary>
        /// Property to setup path for logging file
        /// </summary>
        public static string LoggingFilePath
        {
            get { return m_sLoggingFilePath; }
            set { m_sLoggingFilePath = value; }
        }

        /// <summary>
        /// Property to setup filename for logging file
        /// </summary>
        public static string LoggingFilename
        {
            get { return m_sLoggingFilename; }
            set { m_sLoggingFilename = value; }
        }

        /// <summary>
        /// Write given Message to Message log
        /// </summary>
        /// <param name="Msg">Message to write</param>
        /// <param name="Typ">Message type</param>
        public static void AppendMessage(string Msg, EMessageTypes Typ)
        {
            //run overloaded function 
            AppendMessage(Msg, null, Typ);
        }

        /// <summary>
        /// Write given Message to Message log and append the exception message
        /// </summary>
        /// <param name="Msg">Message to write</param>
        /// <param name="Ex">Exception to write</param>
        /// <param name="Typ">Message type</param>
        public static void AppendMessage(string Msg, Exception Ex, EMessageTypes Typ)
        {
            //run overloaded function 
            AppendMessage(Msg, Ex, false, Typ);
        }

        /// <summary>
        /// Write given Message to Message log and append the exception message.
        /// Additional can show a message box.
        /// </summary>
        /// <param name="Msg">Message to write</param>
        /// <param name="Ex">Exception to write</param>
        /// <param name="ShowDialog">if True is showing a message box</param>
        /// <param name="Typ">Message type</param>
        public static void AppendMessage(string Msg, Exception Ex, bool ShowDialog, EMessageTypes Typ)
        {
            string sMessageText = "";
            //System.IO.StreamWriter myWriter = null;
            Msg = LanguageStrings.GetText(Msg);

            try
            {
                ///Each Message type has a own XML Node
                switch (Typ)
                {
                    case EMessageTypes.Hint:
                        /*
                         * Write a Hint
                         * <MESSAGE><DATE></DATE><TYPE>HINT</TYPE><MESSAGE></MESSAGE></MESSAGE>
                         */
                        sMessageText = "<MESSAGE>";
                        sMessageText += "<DATE>" + DateTime.Now.ToString("yyyyMMdd hh:mm:ss") + "</DATE>";
                        sMessageText += "<TYPE>HINT</TYPE>";
                        sMessageText += "<TEXT>" + Msg + "</TEXT>";
                        sMessageText += "</MESSAGE>";
                        break;
                    case EMessageTypes.Information:
                        /*
                         * Write a Info Message
                         * <MESSAGE><DATE></DATE><TYPE>INFO</TYPE><MESSAGE></MESSAGE></MESSAGE>
                         */
                        sMessageText = "<MESSAGE>";
                        sMessageText += "<DATE>" + DateTime.Now.ToString("yyyyMMdd hh:mm:ss") + "</DATE>";
                        sMessageText += "<TYPE>INFO</TYPE>";
                        sMessageText += "<TEXT>" + Msg + "</TEXT>";
                        sMessageText += "</MESSAGE>";
                        break;
                    case EMessageTypes.Error:
                        /*
                         * Write a Error Message
                         * <MESSAGE><DATE></DATE><TYPE>ERROR</TYPE><MESSAGE></MESSAGE><SOURCE></SOURCE><STACKTRACE></STACKTRACE></MESSAGE>
                         */
                        string sExMessageText = "";
                        string sExMessageSource = "";
                        string sExStackTrace = "";

                        if (Ex != null)
                        {
                            sExMessageText = Ex.Message;
                            Msg +=  " Message: " + Ex.Message;
                            sExMessageSource = Ex.Source;
                            sExStackTrace = Ex.StackTrace;
                        }
                        sMessageText = "<MESSAGE>";
                        sMessageText += "<DATE>" + DateTime.Now.ToString("yyyyMMdd hh:mm:ss") + "</DATE>";
                        sMessageText += "<TYPE>ERROR</TYPE>";
                        sMessageText += "<TEXT>" + Msg + " - " + sExMessageText + "</TEXT>";
                        sMessageText += "<SOURCE>" + sExMessageSource + "</SOURCE>";
                        sMessageText += "<STACKTRACE>" + sExStackTrace + "</STACKTRACE>";
                        sMessageText += "</MESSAGE>\n";



                        Exception myInnerExcetion = null;
                        string myInnerExceptionext = "";

                        if (Ex != null)
                        {
                            myInnerExcetion = Ex.InnerException;

                            while (myInnerExcetion != null)
                            {
                                //myInnerExceptionext = "";

                                sMessageText += "<MESSAGE>";
                                sMessageText += "<DATE>" + DateTime.Now.ToString("yyyyMMdd hh:mm:ss") + "</DATE>";
                                sMessageText += "<TYPE>ERROR</TYPE>";
                                sMessageText += "<TEXT>" + myInnerExcetion.Message + "</TEXT>";
                                sMessageText += "<SOURCE>" + myInnerExcetion.Source + "</SOURCE>";
                                sMessageText += "<STACKTRACE>" + myInnerExcetion.StackTrace + "</STACKTRACE>";
                                sMessageText += "</MESSAGE>\n";
                                //do this for each exception in inner exceptions
                                myInnerExcetion = myInnerExcetion.InnerException;
                                //sMessageText += string.                               
                            }
                        }
                        break;
                    case EMessageTypes.Debug:
                        /*
                         * Write a Debug Message
                         * <MESSAGE><DATE></DATE><TYPE>Debug</TYPE><MESSAGE></MESSAGE></MESSAGE>
                         */
                        sMessageText = "<MESSAGE>";
                        sMessageText += "<DATE>" + DateTime.Now.ToString("yyyyMMdd hh:mm:ss") + "</DATE>";
                        sMessageText += "<TYPE>Debug</TYPE>";
                        sMessageText += "<TEXT>" + Msg + "</TEXT>";
                        sMessageText += "</MESSAGE>";
                        break;
                    default:
                        sMessageText = "<MESSAGE>";
                        sMessageText += "<DATE>" + DateTime.Now.ToString("yyyyMMdd hh:mm:ss") + "</DATE>";
                        sMessageText += "<TYPE>Unknown</TYPE>";
                        sMessageText += "<TEXT>" + Msg + "</TEXT>";
                        sMessageText += "</MESSAGE>";
                        break;
                }

                //m_LogMessages.Add(sMessageText);

                if (m_LoggingWriter == null)
                {
                    string sMyMessageFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                    sMyMessageFilePath = System.IO.Path.Combine(sMyMessageFilePath, LoggingFilePath);

                    if (System.IO.Directory.Exists(sMyMessageFilePath) == false) System.IO.Directory.CreateDirectory(sMyMessageFilePath);

                    m_LoggingWriter = new System.IO.StreamWriter(System.IO.Path.Combine(sMyMessageFilePath, LoggingFilename), true);
                }

                if (m_LoggingWriter.BaseStream != null)
                    m_LoggingWriter.WriteLine(sMessageText); ;

                //if parameter ShowDialog is true then show a dialog on Screen with Message
                if (ShowDialog == true)
                {
                    System.Windows.MessageBox.Show(Msg, "Error", System.Windows.MessageBoxButton.OK);
                }
            }
            catch (Exception Except)
            {
                System.Windows.MessageBox.Show("Unable to write the Message!" +
                                                     "Exception - Message: '" + Except.Message + "'", "Error");
            }
            finally
            {
                //if (m_LoggingWriter != null) m_LoggingWriter.Close();
            }
        }

        public static void CloseLogging()
        {
            try
            {
                if (m_LoggingWriter != null) m_LoggingWriter.Close();
            }
            catch (Exception Ex)
            {
                AppendMessage("Can't close Logging!", Ex, EMessageTypes.Error);
            }
        }
    }
}
